//
//  ZZStatusPictureView.swift
//  SinaWeibo
//
//  Created by QUAN on 15/11/16.
//  Copyright © 2015年 QUAN. All rights reserved.
//

import UIKit

private let PictureCellId = "PictureCellId"
private let PictureCellMargin: CGFloat = 5

class ZZStatusPictureView: UICollectionView {

    // MARK: 图片的URL
    var imageURLs: [NSURL]? {
        didSet{
            reloadData()
            let viewSize = self.claceViewSize()
            self.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(viewSize.width)
                make.height.equalTo(viewSize.height)
            }
        }
    }
    // MARK: 设置图片大小
    private func claceViewSize() -> CGSize{
        
        
        let colum: CGFloat = 3.0000000000001
        let maxWidth = screenW - 2 * StatusCellMargin
        //单个图片宽度
        let itemWidth = (maxWidth - 2 * PictureCellMargin) / colum
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置cell的大小
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        let imageCount = imageURLs?.count ?? 0
        if imageCount  == 0 {
            return CGSizeZero
        }
        
        //单张图片
        if imageCount == 1 {
            
            let imageSize = CGSize(width: 180, height: 120)
            // let key = imageURLs?.last?.7absoluteString
            // let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(key!)
            //
            // let imageSize = image.size
            sizeToFit()
            layout.itemSize = imageSize
            return imageSize
        }
        
        //四张图片
        if imageCount == 4 {
            let w = itemWidth * 2 + PictureCellMargin
            return CGSize(width: w, height: w)
        }
        
        //多图
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let w = maxWidth
        let h = row * itemWidth + (row - 1) * PictureCellMargin
        
        return CGSize(width: w, height: h)
        
    }

    
    // MARK: 重构
    init(){
        let layout = UICollectionViewFlowLayout()
        
        layout.minimumInteritemSpacing = PictureCellMargin
        layout.minimumLineSpacing = PictureCellMargin
        
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        
        setUpUi()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: 设置UI
    private func setUpUi() {
        
        backgroundColor = UIColor.whiteColor()
        
        //注册cell
        registerClass(PictureCell.self, forCellWithReuseIdentifier: PictureCellId)
        
        dataSource = self
    }
    
}

// MARK: 数据源
extension ZZStatusPictureView : UICollectionViewDataSource{
    
    //显示多少item
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    //cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
     
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureCellId, forIndexPath: indexPath) as! PictureCell
        
        cell.backgroundColor = UIColor.randomColor()
        
        cell.url = imageURLs![indexPath.item]
        
        return cell
    }
}
// MARK: 自定义cell
class PictureCell: UICollectionViewCell {
    
    var url : NSURL? {
        didSet{
            imageView.sd_setImageWithURL(url, placeholderImage: nil)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUi()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setUpUi() {
        
        contentView.addSubview(imageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    //懒加载控件
    private lazy var imageView : UIImageView = {
       
        let image = UIImageView()
        
        image.contentMode = .ScaleAspectFill
        
        image.clipsToBounds = true
        
        return image
        
    }()
    
}

